//
//  RHMQSelPointTypeController.m
//  RuiHeAppApp
//
//  Created by Jun on 2023/8/14.
//

#import "RHMQSelPointTypeController.h"
#import "RHMQEndPointModel.h"
#import "RHMQDevRegionModel.h"
#import "RHMQTTManage.h"
#import "RHMQDevModel.h"
#import "SYChartTool.h"

@interface RHMQSelPointTypeController ()<UITableViewDelegate ,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,assign) NSInteger type;

@property (nonatomic ,strong) RHMQEndPointModel *endPointModel;

@property (nonatomic ,strong) NSMutableArray *dataArray;

@property (nonatomic ,assign) __block NSInteger selectIndex;

@property(nonatomic ,copy) NSString *deviceTypeCode;
@end

@implementation RHMQSelPointTypeController

- (instancetype)initWithType{
    self = [super init];
    if(self){
        self.type = 1;
        NSArray *types = @[@"Master",
                           @"Pack",
                           @"EMS",
                           @"PCS",
                           @"DCDCOUT",
                           @"DCDCIN",
                           @"Base",
                           @"FPS",
                           @"LCS",
                           @"AIR",
                           @"MPPT",
                           @"DCP",
                           @"ACP",
                           @"HVB",
                           @"TRANSFORMER",
                           @"STS",
                           @"DCDC",
                           @"TEMP",
                           @"PVDEP",
                           @"INV",
                           @"Meter",
                           @"PVDEV",
                           @"Batt",
                           @"Battery",
                           @"EMU",
                           @"LoadMeter",
                           @"GridMeter",
                           @"PVCAB"];
        [types enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            RHMQEndPointModel *model = [[RHMQEndPointModel alloc] init];
            model.type = obj;
            model.typeCode = obj;
            [self.dataArray addObject:model];
        }];
    }
    return self;
}


- (instancetype)initWithModel:(RHMQEndPointModel *)model{
    self = [super init];
    if(self){
        self.endPointModel = model;
        [self getDevRelation];
    }
    return self;
}

- (NSMutableArray *)dataArray{
    if(!_dataArray){
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"端点选择"];
    [self.view setBackgroundColor:CFTabViewBgColor];
    _selectIndex = -1;
    //[self creatData];
    [self setupUI];
}

//- (void)creatData{
//    
//    NSDictionary *dict = [SYChartTool readLocalJsonFileWithName:@"MqEndPoint"];
//    NSArray *array = [NSArray yy_modelArrayWithClass:[RHMQEndPointModel class] json:dict[@"data"]];
//    
//    kWeakSelf;
//    [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        RHMQEndPointModel *model = obj;
//        [self.dataArray addObject:obj];
//        [model.nodes enumerateObjectsUsingBlock:^(RHMQEndPointModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            [self.dataArray addObject:obj];
//            [obj.nodes enumerateObjectsUsingBlock:^(RHMQEndPointModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//                [self.dataArray addObject:obj];
//                [obj.nodes enumerateObjectsUsingBlock:^(RHMQEndPointModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//                    [self.dataArray addObject:obj];
//                    
//                }];
//            }];
//        }];
//    }];
//    
//    
//    [self.dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        RHMQEndPointModel *end_model = obj;
//        if([end_model.modelCode isEqualToString: self.endPointModel.modelCode]&&[end_model.typeCode isEqualToString: self.endPointModel.typeCode]){
//            weakSelf.selectIndex = idx;
//        }
//    }];
//    
//}

- (void)setupUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [_tableView setBackgroundColor:CFTabViewBgColor];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

#pragma mark - Network
//获取端点关系
- (void)getDevRelation{
    [self.dataArray removeAllObjects];
    [QMUITips showLoadingInView:self.view];
    [RHMQDevRegionModel startGetWithCompletionAPIKey:D_getDevRelation argument:@{@"deviceId":[RHMQTTManage shareMQTTManage].devModel.devId} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
            [self.tableView endHeaderRefresh];
            if(resultModel.code == 200){
                RHMQDevRegionModel *regionModel = resultModel.data;
                self.deviceTypeCode = regionModel.deviceTypeCode;
                if(resultModel.data){
                    [regionModel.data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        RHMQEndPointModel *model = obj;
                        [self.dataArray addObject:obj];
                        [model.nodes enumerateObjectsUsingBlock:^(RHMQEndPointModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                            [self.dataArray addObject:obj];
                            [obj.nodes enumerateObjectsUsingBlock:^(RHMQEndPointModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                                [self.dataArray addObject:obj];
                                [obj.nodes enumerateObjectsUsingBlock:^(RHMQEndPointModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                                    [self.dataArray addObject:obj];
                                }];
                            }];
                        }];
                    }];
                    
                    
                    [self.tableView reloadData];
                }
            }
            
        } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
            
    }];
}



#pragma mark - UITableViewDelegate ,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 30.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    return footView;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell.textLabel setFont:[UIFont systemFontOfSize:14]];
    [cell.textLabel setTextColor:indexPath.row == _selectIndex?CFMainColor:kCOLOR_HEX(@"666666")];
    
    QMUIButton *btn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:indexPath.row == _selectIndex?[UIImage imageNamed:@"submit_success"]:nil forState:UIControlStateNormal];
    [btn setTitleColor:kCOLOR_HEX(@"666666") forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:13]];
    [btn setImagePosition:QMUIButtonImagePositionRight];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [btn setFrame:CGRectMake(0, 0, 23, 15)];
    [cell setAccessoryView:btn];
    
    RHMQEndPointModel *model = self.dataArray[indexPath.row];
    if(self.type == 1){
        [cell.textLabel setText:[NSString stringWithFormat:@"%@",model.type]];
    }else{
        [cell.textLabel setText:[NSString stringWithFormat:@"%@-%@",model.type,model.sn]];
    }
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.selectIndex = indexPath.row;
    [tableView reloadData];
    
    RHMQEndPointModel *model = self.dataArray[indexPath.row];
    if(self.callBackBlock){
        self.callBackBlock([model yy_modelCopy],self.deviceTypeCode);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)dealloc
{
    
}

@end
